<?
/**
 * Various useful fonctions for site ergolangue
 */
 /* Don't change anything below this line                                                        */                                                   

function connect_db()
	{
	global $pass,$login;
	$db=mysql_connect("localhost",$login,$pass) or  die("Unable  to  select  database");
		
	return $db;
	
	}

function db_name()
	{
	global $database_name;
	$db_name=$database_name;
	
	return $db_name;
	
	}
	
#class to compute unique random numbers, to move in an external file eg inc.classes.php
class UniqueRand{
  var $alreadyExists = array();

  function uRand($min = NULL, $max = NULL){
    $break='false';
    while($break=='false'){
      $rand=mt_rand($min,$max);

      if(array_search($rand,$this->alreadyExists)===false){
        $this->alreadyExists[]=$rand;
        $break='stop';
      }else{
        #echo " $rand already!  ";
        #print_r($this->alreadyExists);
      }
    }
    return $rand;
  }
}
$rand=new UniqueRand();

#nettoyer les mots pour le vocabulaire
function normaliserMot($mot) {
	$mot=trim($mot);
	$mot=ereg_replace(",","",$mot);
	$mot=str_replace(",","",$mot);
	$mot=ereg_replace("\.*","",$mot);
	$mot=str_replace("?","",$mot);
	return $mot;
}

#convertir date française DD-MM-YYYY au format MySQL YYYY-MM-DD
function datefr2mySQL($date) { 
$split = explode(".",$date); 
$annee = $split[2]; 
$mois = $split[1]; 
$jour = $split[0]; 
return "$annee"."-"."$mois"."-"."$jour"; 
} 

#convertir date MySQL YYYY-MM-DD  au format français DD-MM-YYYY
function datemySQL2fr($date) { 
$split = explode("-",$date); 
$annee = $split[0]; 
$mois = $split[1]; 
$jour = $split[2]; 
return "$jour"."-"."$mois"."-"."$annee"; 
} 

function date_mysql_to_timestamp($date) {
#if (!preg_match('/(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/',$date,$r)){
#return false;
#}
$ladate=explode("-",$date);
return mktime(0, 0,0,$ladate[2],$ladate[3],$ladate[1] );
}
function verifieDate($date) {
#2007-06-22
#checkdate(m-d-y);
#note: for a strange reason you need firstly to reconstruct the format of date var
$ladate1=explode("-",$date);
$month=ereg_replace("^0","",$ladate1[1]);
$day=ereg_replace("^0","",$ladate1[2]);
$year=$ladate1[0];
return checkdate($month, $day, $year);
}

function testSql($sql) {
	if(!$sql) {
		echo "Error MySQL: " .mysql_error(); exit; //there is an error, notify and exit
	}
echo "<pre>" .$sql ."</pre>";
echo "Results items: " .mysql_num_rows(mysql_query($sql)); //how many items
exit;
}
/* VARIOUS SQL QUERIES
 */
function animateur($id) {
$sqlQuery = "SELECT *  FROM animateurs WHERE id_animateur = '$id'";
$result = MYSQL_QUERY($sqlQuery);
$numberOfRows = MYSQL_NUM_ROWS($result);
return $sql;
#return ($sql,$numberOfRows);
}

function probleme() {
echo 	"<br><input type=\"button\" name=\"cancel\" value=\"Retour\" onClick=\"javascript:history.back();\">";
	exit;
}

 ?>